package com.study.aop.theory.myaop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author: chenhs
 * @date: Created in 16:19 2019/12/8
 **/
public class AopInvocationHandler implements InvocationHandler {

    private Object target;

    private Aspect aspect;

    public AopInvocationHandler(Object target, Aspect aspect) {
        this.target = target;
        this.aspect = aspect;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //判断当前方法是否需要增强
        if (method.getName().matches(this.aspect.getPointcut().getMethodPattern())) {
            return this.aspect.getAdvice().invoke(target, method, args);
        }

        //不需要增强，则直接调用业务对象方法
        return method.invoke(target, args);
    }
}
